﻿@model IniTechCaash.WebApplication.Models.JournalEntryCreateViewModel

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>

@using (Html.BeginForm("Create", "Journal", FormMethod.Post, new { @enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken()
    
        <div class="form-horizontal">
        <h4>New Journal Entry</h4>
        <hr />
        @Html.ValidationSummary(true)

        <div class="form-group">
            @Html.LabelFor(model => model.Name, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Name)
                @Html.ValidationMessageFor(model => model.Name)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.SourceDocument, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                <input name="SourceDocument" id="SourceDocument" type="file" />
                @Html.ValidationMessageFor(model => model.SourceDocument)
            </div>
        </div>

        <table class="table">
            <thead>
                <tr>
                    <th>#</th>
                    <th>Account</th>
                    <th>Amount</th>
                    <th>Side</th>
                    <th>Date</th>
                    <th>Description</th>
                </tr>
            </thead>
            <tbody id="lineItems">
                @Html.EditorFor(model => model.Lines)
            </tbody>
        </table>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" id="addLineItem" value="Add Line Item" class="btn btn-default" />
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts
{
<script language="javascript" src="@BundleTable.Bundles.ResolveBundleUrl("~/Scripts/Forms")" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
//<![CDATA[

$(document).ready(function () {

    // attach events
    $('#addLineItem').on('click', function (event) {
        event.preventDefault();
        addLineItem();
    });
    $('#lineItems tr td.remove span button').on('click', function () {
        removeLineItem($(this).closest('tr')[0].sectionRowIndex);
    });
    $('#lineItems tr td.side select').change(function () {
        orderLineItems();
    });

    refreshLineItems();
});

function addLineItem() {
    var row = $('#lineItems tr:last').clone(true, true);
	//row.find('input').removeAttr('disabled');
    //row.find('input[type=checkbox]').attr('checked', 'checked');
    //row.find('input[type=hidden]').val('false');
    //row.find('input[type=text]').not('.date').val('');
    //row.find('input.masked').each(function () {
        //$(this).removeClass('masked').inputmask($(this).attr('data-input-mask'), {
            //placeholder: ' '
        //}).addClass('masked');
    //});
    row.appendTo('#lineItems');
    refreshLineItems();
}

function orderLineItems() {
    var table = $('#lineItems');
    var rows = table.children('tr');
    rows.detach().sort(function (a, b) {
        var aSide = $(a).find('td.side select option:selected').text();
        var bSide = $(b).find('td.side select option:selected').text();
        if (aSide == bSide)
            return 0;
        if (aSide == 'Debit' && bSide == 'Credit')
            return -1;
        else
            return 1;
    });
    table.append(rows);

    refreshLineItems();
}

function refreshLineItems() {
    var rowIndex = 1;
    $('#lineItems tr').each(function () {
        $(this).find('td.sequence').html(rowIndex);
        $(this).find('input, select, textarea').each(function () {
            var name = $(this).attr('name');
            name = name.replace(/\[[0-9]*\]/g, '[' + (rowIndex - 1) + ']');
            $(this).attr('name', name);
        });
        if ($(this).find('td.side select option:selected').text() == 'Credit') {
            $(this).find('td.indentable').each(function () {
                $(this).addClass('credit-indent');
            })
        }
        else {
            $(this).find('td.indentable').each(function () {
                $(this).removeClass('credit-indent');
            })
        }
        rowIndex++;
    });

    if ($('#lineItems tr').length == 2) {
        $('#lineItems tr td.remove span').hide();
    } else {
        $('#lineItems tr td.remove span').show();
    }

    $('#lineItemsSummary').empty();
}

function removeLineItem(index) {
    $('#lineItems tr:eq(' + index + ')').remove();
    refreshLineItems();
}

//]]>
</script>
}
